package com.sea.controller;

import com.sea.pojo.Article;
import com.sea.pojo.PageBean;
import lombok.extern.slf4j.Slf4j;
import com.sea.pojo.Result;
import com.sea.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService as;

    @PostMapping
    public Result addA(@RequestBody @Validated Article a) {
        log.info(String.valueOf(a));
        as.add(a);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb = as.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article a = as.finda(id);
        return Result.success(a);
    }

    @PutMapping
    public Result updateA(@RequestBody Article a) {
        as.updatea(a);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteA(Integer id) {
        as.dlt(id);
        return Result.success();
    }
}
